package db_tool;

import org.apache.struts2.ServletActionContext;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class User {
	private String ID;
	//用户表字段
	private String username;
	private String passwold;
	private String Telephone;
	private String Email;
	private String Description;
	private String active;
	private String superiorName;
	private String roleName;
	private PrintWriter writer = null;
	
	public String getID() {
		return ID;
	}
	public void setID(String ID) {
		this.ID = ID;
	}
    public String getActive1()throws Exception{
    	DBConnection db_connection = DBConnection.getInstance();
    	writer = ServletActionContext.getResponse().getWriter();
		Statement statement = null;
		statement = db_connection.getStatement();
//		String sql="UPDATE RegisterAccount SET active='yes' where id='" + ID+ "'";
		
		String sql="select * from RegisterAccount where RegID='" + ID+ "'";
		ResultSet rs=statement.executeQuery(sql);
		while (rs.next()) {
			System.out.println(rs.getString("RegID"));
			//字段赋值
			username=rs.getString("username");
			passwold=rs.getString("passwold");
			Telephone=rs.getString("Telephone");
			Email=rs.getString("Email");
			Description="普通用户";
			active="yes";
			superiorName="无";
			roleName="common";
		}
		String sql1="insert into Account values('"+username+"','"+passwold+"','"+Telephone+"','"+Email+"',";
		String sql2="'"+Description+"','"+active+"','"+superiorName+"','"+roleName+"')";
		String sqli=sql1+sql2;
		
		String sqlu="UPDATE RegisterAccount SET active='yes' where RegID='" + ID+ "'";
		statement.executeUpdate(sqlu);//更改用户状态
		
		try{
			statement.execute(sqli);//插入用户表
			writer.write("success");
		}catch(SQLException e){
			writer.write("error");
			System.out.println("--插入错误--");
			e.printStackTrace();
		}
		finally{
			try {
				db_connection.close(statement);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println("插入操作关闭失败");
				e.printStackTrace();
			}
			writer.flush(); 
			writer.close();
		}
		return null;
    }
}